<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <link rel="stylesheet" href="carou.css">
</head>

<body>
    <div class="container">
        <div class="wrap">
            <img src="http://p1.music.126.net/A4oyg7nydQk9na3fCE7ZDg==/109951165421617285.jpg?imageView&quality=89" alt="">
        </div>
        <div class="buttons">
            <span class="on" onclick="showImg(this)">1</span>
            <span onclick="showImg(this)">2</span>
            <span onclick="showImg(this)">3</span>
            <span onclick="showImg(this)">4</span>
            <span onclick="showImg(this)">5</span>
            <span onclick="showImg(this)">6</span>
        </div>
        <div class="arrow">
            <!-- 特殊符号 &lt; <  &gt;> -->
            <a href="javascript:;" class="arrow_left"> &lt;</a>
            <a href="javascript:;" class="arrow_right">&gt;</a>
        </div>
    </div>
    <script>
        // 数组的图片地址
        let img_arr = ["http://p1.music.126.net/A4oyg7nydQk9na3fCE7ZDg==/109951165421617285.jpg?imageView&quality=89",
        "http://p1.music.126.net/c27byhx3KVvCle1zykmH_A==/109951165421216056.jpg?imageView&quality=89",
        "http://p1.music.126.net/Mot-Vbz3JxqI2zbLzye0lQ==/109951165421632886.jpg?imageView&quality=89",
        "http://p1.music.126.net/uU3uL1XvXk7UieISRivS2g==/109951165421640213.jpg?imageView&quality=89",
        "http://p1.music.126.net/Mot-Vbz3JxqI2zbLzye0lQ==/109951165421632886.jpg?imageView&quality=89",
        "http://p1.music.126.net/KLyKvFgCMO68juLWVCgD4A==/109951165421399234.jpg?imageView&quality=89"]
        // 图片的地址下标
        let index = 0;
        // 循环器的id
        let time = null;
        let container = document.querySelector(".container")
        let wrap = document.querySelector(".wrap")
        let span_arr = document.querySelectorAll(".buttons span")
        // 选择图片的节点
        let img = document.querySelector("img")
        let arrow = document.querySelector(".arrow")
        let arrow_left = document.querySelector(".arrow_left")
        let arrow_right = document.querySelector(".arrow_right")
        // 左右括号显示
        container.addEventListener("mouseover", function (event) {
            arrow.style.display = "block";
            // 关闭循环器
            clearInterval(time);
        })
        container.addEventListener("mouseout", function (event) {
            arrow.style.display = "none";
            //启动自动播放
            autoPlay();
        })
        // 左按钮 添加click方法
        let pre = function () {
            index = --index;
            if (index < 0) {
                index = 5;
            }
            img.src = img_arr[index];
            showNum(index)
        }
        arrow_left.addEventListener("click", pre);
        // 右按钮 添加click方法
        let next = function () {
            index = ++index;
            if (index > 5) {
                index = 0;
            }
            img.src = img_arr[index];
            showNum(index)
        }
        arrow_right.addEventListener("click", next);
        // 显示图片的数字圆点
        function showNum(index) {
            // 去掉span的on名称
            document.querySelector(".on").classList.remove("on");
            // 给当前点击的span 添加on
            span_arr[index].classList.add("on");
        }
        function showImg(ele) {
            let num = Number.parseInt(ele.innerText)
            index = --num;
            img.src = img_arr[index];
            showNum(index)
        }
        function autoPlay(){
            time = setInterval(next, 1000);
        }
    </script>
</body>

</html>